﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Mny2019r
/// 파  일  명 : Mny2019r.aspx.cs
/// 설      명 : 연간예산대비지출
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-12-18 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.Dac;
using Updms.Mny.Exe;


namespace Updms.Mny
{
	public partial class Mny2019r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mny2019r";
		public static readonly string gs_path_info = "/updms/Mny/" + PID + ".aspx";

		Global global = new Global();

		//검색
		protected string kw1 = "";	// 기준년도

		//메뉴 선택값
		protected string gs_sm = "";	

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetBaseYear();
			SetMonthlyOutAmountByBudgetLabel();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion				

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			kw1 = ddl_search_base_year.SelectedValue;
		}

		#endregion

		#region 마스터 Method (SetBaseYear, SetMonthlyOutAmountByBudget, 전년, 당년, 차년)

		//기준년도 설정
		private void SetBaseYear()
		{
			ddl_search_base_year.Items.Clear();

			int li_f_year = Convert.ToInt32(SEARCH_Y_MIN);
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(1, "yyyy"));
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			ddl_search_base_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년
		}

		//월별예산대비지출현황조회 라벨
		private void SetMonthlyOutAmountByBudgetLabel()
		{
			string ls_data = "";
						
			for (int i = 1; i <= 12; i++)
			{
				string ls_base_month = i < 10 ? "0"+i.ToString() : i.ToString();
				string ls_base_ym = ddl_search_base_year.SelectedValue + "-" + ls_base_month;
				ls_data += "<span class=\"month_box_y\" title=\"" + i.ToString() + "월\" onclick=\"showOutByBudgetPage('" + ls_base_ym + "')\">" + i.ToString() + "</span>&nbsp;";
			}

			lbl_monthly_budget_list.Text = ls_data;
		}

		//전년, 차년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			string ls_year = ddl_search_base_year.SelectedValue;
			string ls_calc_year = Convert.ToString(Convert.ToInt32(ls_year) + pi_year_cnt);

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_year))
			{
				ddl_search_base_year.SelectedValue = ls_calc_year;
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");
			ddl_search_base_year.SelectedValue = ls_this_year;
			SetEnabledYearBtn();
		}

		//전년, 차년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled = (ddl_search_base_year.SelectedIndex == 0) ? false : true;
			ibtn_next_year.Enabled = (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		#endregion	

		#region 리스트 (SelectProcess, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_base_year", typeof(string), kw1);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2019r_NT _Mny2019r_NT = new Mny2019r_NT())
			{
				lo_ds.Merge(_Mny2019r_NT.GetYearlyOutAmountListByBudget(lo_dp));
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}		

		//검색
		private void SearchProcess()
		{
			SetSearchParameter();
			SelectProcess();
			SetMonthlyOutAmountByBudgetLabel();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;
			DataSet lo_ds = new DataSet();			

			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//예산 - 지출
				switch (gv.DataKeys[e.Row.RowIndex].Values["Flag"].ToString())
				{
					case "BA006": //예산 - 지출
						e.Row.Attributes.Add("class", "sub_total_v1");
						break;
					default:
						break;
				}

				//총계
				switch (gv.DataKeys[e.Row.RowIndex].Values["Account"].ToString() + "_" + gv.DataKeys[e.Row.RowIndex].Values["Flag"].ToString())
				{
					case "BA002_BA004": //총계(예산)
						e.Row.Attributes.Add("class", "total_v1");
						for (int i = 0; i < e.Row.Cells.Count; i++)
						{
							e.Row.Cells[i].Attributes.Add("class", "div_line_t_dbl");
						}
						break;
					case "BA002_BA005": //총계(지출)
						e.Row.Attributes.Add("class", "total_v1");
						break;
					case "BA002_BA006": //총계(예산 - 지출)
						e.Row.Attributes.Add("class", "total_v3");
						break;
					default:
						break;
				}

				//마이너스는 빨강색
				for (int i = 4; i < 19; i++)
				{
					if (e.Row.Cells[i] != null)
					{
						if (e.Row.Cells[i].Text.Substring(0, 1) == "-")
						{
							e.Row.Cells[i].Attributes.Add("class", "color_red");
						}
					}
				}
			}
			//머지
			SMGridView sGv = new SMGridView();
			sGv.RowSpanGridView(GridView1, 1);	//2.Class_Nm
			sGv.RowSpanGridView(GridView1, 4);	//4.Account_Nm
		}

		#endregion

		#region 버튼이벤트

		//전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		//차년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledYearBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_year", typeof(string), kw1);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2019r_NT _Mny2019r_NT = new Mny2019r_NT())
			{
				lo_ds.Merge(_Mny2019r_NT.GetYearlyOutAmountListByBudget(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		#endregion

	}
}
